<?php

require_once('DB_MysqlStatement.php');

class DB_Mysql {
    protected $user;
    protected $pass;
    protected $dbhost;
    protected $dbname;
    protected $dbh; // handler de la BBDD
    
    public function __construct($user, $pass, $dbhost, $dbname) {
        $this->user = $user;
        $this->pass = $pass;
        $this->dbhost = $dbhost;
        $this->dbname = $dbname;
    }
    
    protected function connect() {
        $this->dbh = mysql_connect($this->dbhost, $this->user, $this->pass);
        
        if (!is_resource($this->dbh)) throw new Exception;
        
        if (!mysql_select_db($this->dbname, $this->dbh)) throw new Exception;
    }
    
    public function prepare($query) {
        if (!$this->dbh) $this->connect();
        $result = new DB_MysqlStatement($this->dbh, $query);
        if (!mysql_close($this->dbh)) throw new exception("ERROR: Desconexi&oacute; a la BBDD.");
        return $result;
    }
}
?>
